<template>
  <div>
    <li>
      <label>
        <input :checked="item.done" type="checkbox" @change="changeDone(item)"/>
        <input v-if="flag" ref="text" v-model="title" type="text" @blur="updateNow(item)"
               @keydown.enter="updateNow(item)"/>
        <span v-show="!flag">{{ item.title }}</span>
      </label>
      <button class="btn btn-danger" @click="deleteObj(item)">删除</button>
      <button v-if="btnflag" class="btn btn-danger2" @click="updateObj">编辑</button>
    </li>
  </div>

</template>

<script>
export default {
  name: "MyItem",
  props: ["item"],
  data() {
    return {
      flag: false,
      btnflag: true,
      title: this.item.title
    }
  },
  methods: {
    updateObj() {
      this.flag = true;
      this.btnflag = false;
      this.$nextTick(function () {
        this.$refs.text.focus()
      })

    },
    deleteObj(val) {
      this.$bus.$emit("deleteObj", val);
    },
    changeDone(val) {
      let a = val;
        a.done = !a.done
      this.$bus.$emit("changeDone", a);
    },
    updateNow(item) {
      let a = item;
      this.btnflag = true;
      this.flag = false;
      a.title = this.title
      this.$bus.$emit("changeDone", a);
    }
  }
}
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

li:hover {
  background-color: #ddd;
}

li:hover button {
  display: block;
}
</style>
